package com.example.helloworld.tab.act4;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.helloworld.R;

public class MyListAdapter extends BaseAdapter{
	private List<Activity4Item> datas;  
    Context context;
    public MyListAdapter(Context context,List<Activity4Item> datas){  
        this.datas = datas;  
        this.context = context;  
    }
	@Override
	public int getCount() {
		return datas==null?0:datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	 public class ViewHolder{  
	        TextView textViewItem01;  
	        TextView textViewItem02;  
	        TextView textViewItem03;  
	        ImageView imageView;  
	    }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Activity4Item tmp = (Activity4Item)getItem(position);
		 ViewHolder viewHolder = null;  
	        if(convertView==null){  
	            Log.d("MyBaseAdapter", "新建convertView,position="+position);  
	            convertView = LayoutInflater.from(context).inflate(  
	                    R.layout.tab4_item, null);  
	              
	            viewHolder = new ViewHolder();  
	            viewHolder.textViewItem01 = (TextView)convertView.findViewById(  
	                    R.id.listView01Item01);  
	            viewHolder.textViewItem02 = (TextView)convertView.findViewById(  
	                    R.id.listView01Item02);  
	            viewHolder.textViewItem03 = (TextView)convertView.findViewById(  
	                    R.id.listView01Item03);  
	              
	            //动态增加1个ImageView  
	            viewHolder.imageView = new ImageView(context);  
	            LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(  
	                    LinearLayout.LayoutParams.WRAP_CONTENT,  
	                    LinearLayout.LayoutParams.WRAP_CONTENT);  
	            mParams.gravity = Gravity.CENTER;  
	            mParams.width=50; 
	            mParams.leftMargin=40;
	            mParams.rightMargin=15;
	            viewHolder.imageView.setLayoutParams(mParams);  
	            //这个ImageView放到ListView的第2列之后  
	            ((LinearLayout)convertView).addView(viewHolder.imageView,0);  
	              
	            convertView.setTag(viewHolder);  
	        }else{  
	            viewHolder = (ViewHolder)convertView.getTag();  
	            Log.d("MyBaseAdapter", "旧的convertView,position="+position);  
	        }  
	          
	        viewHolder.textViewItem01.setText(tmp.getId());  
	        viewHolder.textViewItem02.setText(tmp.getTitle());  
	        viewHolder.textViewItem03.setText(tmp.getDescription());  
	        viewHolder.imageView.setImageResource(R.drawable.ic_launcher);  
	          
	        //对ListView中第1个TextView配置OnClick事件  
	        viewHolder.textViewItem01.setOnClickListener(new OnClickListener(){  
	            @Override  
	            public void onClick(View v) {  
	                Toast.makeText(context,   
	                        "[id]点击了"+tmp.getTitle()+",但是功能还没有",   
	                        Toast.LENGTH_SHORT).show();  
	            }  
	        });  
	          
	        //对ListView中的每一行信息配置OnClick事件  
	        convertView.setOnClickListener(new OnClickListener(){  
	  
	            @Override  
	            public void onClick(View v) {  
	                Toast.makeText(context,   
	                        "[view]点击了"+tmp.getTitle()+",但是功能还没有",   
	                        Toast.LENGTH_SHORT).show();  
	            }  
	              
	        });  
	          
	        //对ListView中的每一行信息配置OnLongClick事件  
	        convertView.setOnLongClickListener(new OnLongClickListener(){  
	            @Override  
	            public boolean onLongClick(View v) {  
	                Toast.makeText(context,   
	                        "[view setOnLongClickListener]点击了"+tmp.getTitle()+",但是功能还没有",   
	                        Toast.LENGTH_SHORT).show();  
	                return true;  
	            }  
	        });  
	          
	        return convertView;  
	}
	
}